//
//  ZBBProvinceViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBProvinceViewController.h"
#import "ZBBLocationViewControllViewCell.h"
#import "TitleCollectionViewCell.h"
#import "ZBBTitleCollectionViewCell.h"
#import "FitHeader.h"
#import "TwoToOne.h"
#import "ZBBProVinceModel.h"
#import "ZBBProvinceControllViewCell.h"
#import "ZBBProVinceCell.h"
#import "TabBarController.h"
@interface ZBBProvinceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
@property(nonatomic,strong)UICollectionView *titleCollectionView;
@property(nonatomic,strong)UICollectionView *contentCollectionView;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic, strong)NSMutableArray *contentArray;
@property(nonatomic, strong)NSMutableDictionary *dataSourceDictionary;
@property(nonatomic, strong)UICollectionView *provinceCollectionView;
@property(nonatomic, strong)UIView *provinceView;
@property(nonatomic, strong)UIButton *arrowButton;
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, assign)NSInteger flag1;
@property(nonatomic, assign)NSInteger temp;
@property(nonatomic, strong)NSString *provinceSting;
@property(nonatomic, strong)UILabel *transitionLabel;

@end

@implementation ZBBProvinceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
     self.title = @"省市台";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *titleFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    titleFlowLayout.minimumInteritemSpacing = 0;
    titleFlowLayout.minimumLineSpacing = 0;
//    titleFlowLayout.itemSize = CGSizeMake(80, 50);
    titleFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    titleFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _titleCollectionView.showsHorizontalScrollIndicator = NO;
    _titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 60 * FITWidth, 50 * FITHeight) collectionViewLayout:titleFlowLayout];
    
    
    [_titleCollectionView registerClass:[ZBBTitleCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    _titleCollectionView.pagingEnabled = NO;
    _titleCollectionView.dataSource = self;
    _titleCollectionView.delegate = self;
    _titleCollectionView.layer.borderWidth = 0.5;
    _titleCollectionView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self.view addSubview:_titleCollectionView];
    
    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];
    
    contentFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 * FITHeight, self.view.frame.size.width, self.view.frame.size.height - 64 - 50 * FITHeight) collectionViewLayout:contentFlowlayout];
    [self.view addSubview:_contentCollectionView];
    _contentCollectionView.delegate = self;
    _contentCollectionView.dataSource = self;
    _contentCollectionView.pagingEnabled = YES;
    [_contentCollectionView registerClass:[ZBBProvinceControllViewCell class] forCellWithReuseIdentifier:@"content"];
    _titleCollectionView.backgroundColor = [UIColor whiteColor];
    _contentCollectionView.backgroundColor = [UIColor whiteColor];
    
    
    UICollectionViewFlowLayout *provinceLayout = [[UICollectionViewFlowLayout alloc]init];
    provinceLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    provinceLayout.minimumInteritemSpacing = 5;
    provinceLayout.minimumLineSpacing = 5;
 
    
    
    self.provinceCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 55, self.view.frame.size.width,260) collectionViewLayout:provinceLayout];
    _provinceCollectionView.scrollEnabled = NO;
    _provinceCollectionView.delegate = self;
    _provinceCollectionView.dataSource = self;
  
    [self.view addSubview:_provinceCollectionView];
    [_provinceCollectionView registerClass:[ZBBProVinceCell class] forCellWithReuseIdentifier:@"province"];
    _provinceCollectionView.backgroundColor = [UIColor whiteColor];
    
    _provinceCollectionView.hidden = YES;
    [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
   [_arrowButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.arrowButton];
 
       
    
}
-(void)buttonAction
{
    
    if (_temp) {
        [UIView animateWithDuration:1 animations:^{
            _provinceCollectionView.hidden = NO;
              [self.arrowButton setImage:[UIImage imageNamed:@"shang.png"] forState:UIControlStateNormal];
            self.transitionLabel.hidden = NO;
        }];
        _temp = 0;
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            _provinceCollectionView.hidden = YES;
              [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
                   }];
        _temp = 1;
         self.transitionLabel.hidden = YES;
    }
}
-(UILabel *)transitionLabel
{
    
    if (_transitionLabel == nil) {
        _transitionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 335, 50)];
        [self.view addSubview:_transitionLabel];
        _transitionLabel.text = @"切换省市台";
        _transitionLabel.font = [UIFont systemFontOfSize: 13];
        _transitionLabel.textColor = [UIColor grayColor];
        _transitionLabel.backgroundColor = [UIColor whiteColor];
    }
    return _transitionLabel;
}
-(UIButton*)arrowButton
{
    
    if (_arrowButton == nil) {
        
       _arrowButton = [UIButton buttonWithType:UIButtonTypeSystem];
       _arrowButton.frame = CGRectMake(335, 15, 16, 16);
    }
    return _arrowButton;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _titleCollectionView) {
        return _titleArray.count;
    }
    else if(collectionView == _contentCollectionView)
    {
        return _titleArray.count;
    }
    else
    {
        return _titleArray.count;
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (collectionView == _titleCollectionView) {
        return CGSizeMake(80, 50);
    }
    else if(collectionView == _contentCollectionView)
    {
        return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height  - 64 - 50 *FITHeight);
    }
    else
    {
        return CGSizeMake(65, 30);
    }
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _titleCollectionView) {
       ZBBTitleCollectionViewCell *titleCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
        
        titleCell.model = _titleArray[indexPath.row];
        
        
        if (_flag == indexPath.row) {
            titleCell.lineLabel.backgroundColor = [UIColor redColor];
            titleCell.titleLabel.textColor = [UIColor redColor];
        }
        else
        {
            titleCell.lineLabel.backgroundColor = [UIColor whiteColor];
            titleCell.titleLabel.textColor = [UIColor blackColor];
        }
        return titleCell;
    }
    else if(collectionView == _contentCollectionView)
    {
        
        ZBBProvinceControllViewCell *contentCell = [collectionView  dequeueReusableCellWithReuseIdentifier:@"content" forIndexPath:indexPath];
        contentCell.backgroundColor = [UIColor redColor];
        ZBBProVinceModel *model = _titleArray[indexPath.row];
        if (_flag1) {
            contentCell.urlString = [_dataSourceDictionary objectForKey:_provinceSting];
        }
        else
        {
            contentCell.nav = self.navigationController;
            contentCell.urlString = [_dataSourceDictionary objectForKey:model.provinceName];
        }
        
        return contentCell;
    }
    else if(collectionView == _provinceCollectionView)
    {
        ZBBProVinceCell *provinceCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"province" forIndexPath:indexPath];
        if (_titleArray.count != 0) {
            ZBBProVinceModel *model = _titleArray[indexPath.row];
            provinceCell.label.text = model.provinceName;
        }
        
        return provinceCell;
    }
    
    
    
    
    return nil;
    
}
-(void)getData
{
    self.titleArray = [NSMutableArray array];
    
    self.dataSourceDictionary = [NSMutableDictionary dictionary];
    [TwoToOne getDataByString:@"http://live.ximalaya.com/live-web/v1/getProvinceList?device=android" BodyString:nil WithDataBlock:^(id data) {
        NSDictionary *array = [data objectForKey:@"result"];
        
        for (NSDictionary *dic in array) {
            ZBBProVinceModel *model = [[ZBBProVinceModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [_titleArray addObject:model];
            [self.dataSourceDictionary setObject:model.provinceCode forKey:model.provinceName];
            
        }
        
        [_titleCollectionView reloadData];
        [_contentCollectionView reloadData];
        [_provinceCollectionView reloadData];
    }];
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        _flag = indexPath.row;
        _flag1 = 1;
        [_contentCollectionView setContentOffset:CGPointMake(self.view.frame.size.width * indexPath.row, 0) animated:YES];
        [_titleCollectionView  setContentOffset:CGPointMake(80 *_flag , 0) animated:YES];
        
        ZBBProVinceModel *model = _titleArray[indexPath.row];

        _provinceSting = model.provinceName;
       
        
        [_contentCollectionView reloadData];
        
        [_titleCollectionView reloadData];

    }

   else if (collectionView == _provinceCollectionView)
            {
                [UIView animateWithDuration:1 animations:^{
                    _provinceCollectionView.hidden = YES;
                    [self.arrowButton setImage:[UIImage imageNamed:@"xia.png"] forState:UIControlStateNormal];
                    
                    self.transitionLabel.hidden = YES;
                }];

                _flag = indexPath.row;
                _flag1 = 1;
                [_contentCollectionView setContentOffset:CGPointMake(self.view.frame.size.width * _flag , 0) animated:YES];
                [_titleCollectionView  setContentOffset:CGPointMake(80 *_flag, 0) animated:YES];
                ZBBProVinceModel *model = _titleArray[indexPath.row];
                _provinceSting = model.provinceName;
                [_titleCollectionView reloadData];
                [_contentCollectionView reloadData];
                
}
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_contentCollectionView == scrollView) {
        _flag = scrollView.contentOffset.x / self.view.frame.size.width;
        [_titleCollectionView reloadData];
         [_titleCollectionView  setContentOffset:CGPointMake(80 *_flag, 0) animated:YES];
        
        ZBBProVinceModel *model = _titleArray[_flag];
        
        _provinceSting = model.provinceName;
        [_contentCollectionView reloadData];

    }
}
-(void)viewWillAppear:(BOOL)animated
{
    
    [[TabBarController shareInstance] remainButtonOfPlay:YES];
    
}

-(void)viewDidDisappear:(BOOL)animated
{
    [[TabBarController shareInstance] remainButtonOfPlay:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
